$(function() {
	//点击返回按钮返回上一页
	$(".goBack").bind("click", function() {
		//		window.history.back();location.reload();	//强行刷新
		//		window.history.go(-1);  //返回上一页
		window.location.href = document.referrer; //返回上一页并刷新
	});

	//点击全部订单跳转到全部订单界面
	$(".order_ul .all").bind("click", function() {
		window.location.href = "allOrder.html"
	});
	//点击待付款跳转到待付款界面
	$(".order_ul .wait_pay").bind("click", function() {
		window.location.href = "waitPayOrder.html"
	});
	//点击待发货跳转到待发货界面
	$(".order_ul .wait_deliver").bind("click", function() {
		window.location.href = "waitDeliverOrder.html"
	});
	//点击待收货跳转到待收货界面
	$(".order_ul .wait_receipt").bind("click", function() {
		window.location.href = "waitReceiptOrder.html"
	});

	//点击订单获取订单号
	$(".orderList_box").bind("click", function() {
		var orderNum = $(this).find(".orderNum").text();
		console.log(orderNum);
	});
	//点击申请退货按钮的事件，首先阻止事件冒泡
	$(".return_goods").bind("click", function(e) {
		var evt = e || window.event; //兼容IE
		evt.stopPropagation();
		console.log("已经阻止事件冒泡");
		var OdNum = $(this).parents(".orderList_box").find(".orderNum").text();
		console.log(OdNum);
	});

	// 获取零元购商城和集分商城的订单总页数
	var zeroPage = 0; // 零元购商城的订单总页数
	var jfscPage = 0; // 集分商城的订单总页数
	var flag1 = true;
	var flag2 = true;
	var zeroCode = 1;
	var JfscCode = 1;

	function getZeroPage(a) {
		return zeroPage = a;
	}

	function getJFSCPage(b) {
		return jfscPage = b;
	}

	function compare(a, b) {
		return a > b ? a : b;
	}

	function Flag1(a) {
		return flag1 = a;
	}

	function Flag2(a) {
		return flag2 = a;
	}
	function getZeroCode(a){
		return zeroCode = a;
	}
	function getJfscCode(b){
		return JfscCode = b;
	}
	// 加载零元购商城和集分商城的订单
	dropload();

	function dropload() {
		var page = 0; //页码数
		// dropload函数接口设置
		$('.goodOrder').dropload({
			scrollArea: window,
			// 上拉加载更多
			domDown: {
				domClass: 'dropload-down',
				// 滑动到底部显示内容
				domRefresh: '<div class="dropload-refresh">上拉加载更多</div>',
				// 内容加载过程中显示内容
				domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
				// 没有更多内容显示提示
				domNoData: '<div class="dropload-noData">我也是有深度的...</div>'
			},
			// 上拉加载更多 回调函数
			loadDownFn: function(me) {
				page++; // 每次请求，页码加1
				if(flag1) {
					var load1 = $.ajax({
						type: 'post',
						url: 'http://a.91jfk.com/Product/Order/zero_index?p=' + page,
						data: {
							state: 6
						},
						dataType: 'json',
						success: function(res) {
							console.log(res);
							getZeroCode(res.code);
							getZeroPage(res.totalpage);
							// data = JSON.parse(res);
							if(res.nowpage < res.totalpage) {
								Flag1(true);
								if(res.code == '1') {
									var data = res.msg;
									if(data.length > 0) {
										for(var i = 0; i < data.length; i++) {
											if(data[i].order_state == '退货申请') {
												zeroturnBack(data[i].orderid, data[i].order_state, data[i].product,res.nowpage, data[i].product.length, data[i].total);
											}
											$('img.lazy'+res.nowpage).lazyload({
		                                        placeholder:"/Public/Product/img/loading2.gif",
		                                        effect: "fadeIn",
		                                        container: $(".allOrderList")
		                                    });
										}
									}
									return_goods(); // 申请退换货
									if(data.length < 3) {
										// 显示无数据
										me.noData();
										// 重置
										setTimeout(function() {
											me.resetload();
										}, 1500);
									}
									setTimeout(function() {
										// 重置
										me.resetload();
									}, 1500);
								} else {
									me.noData();
									// 重置
									setTimeout(function() {
										me.resetload();
									}, 1500);
								}
							} else {
								load1.abort();
								Flag1(false);
								if(res.code == '1') {
									var data = res.msg;
									for(var i = 0; i < data.length; i++) {
										if(data[i].order_state == '退货申请') {
											zeroturnBack(data[i].orderid, data[i].order_state, data[i].product,res.nowpage, data[i].product.length, data[i].total);
										}
										$('img.lazy'+res.nowpage).lazyload({
	                                        placeholder:"/Public/Product/img/loading2.gif",
	                                        effect: "fadeIn",
	                                        container: $(".allOrderList")
	                                    });
									}
									return_goods(); // 申请退换货
									if(data.length < 3) {
										// 显示无数据
										me.noData();
										// 重置
										setTimeout(function() {
											me.resetload();
										}, 1500);
									}
								} else {
									me.noData();
                                    me.lock();
									// 重置
									setTimeout(function() {
										me.resetload();
									}, 1500);
								}
							}
							// 重置
							setTimeout(function() {
								me.resetload();
							}, 1500);
						},
						// 加载出错
						error: function(err) {
							console.log('Ajax error!');
							// 即使加载出错，也得重置
							setTimeout(function() {
								me.resetload();
							}, 1500);
						}
					});
				} else {
					var max = compare(zeroPage, jfscPage);
					if(page > max) {
						// 再往下已经没有数据，锁定
						// 显示无数据
						me.noData();
						me.lock();
						// 重置
						setTimeout(function() {
							me.resetload();
						}, 1500);
					} else {
						me.unlock();
					}
				}
				if(flag2) {
					var load2 = $.ajax({
						type: 'post',
						url: 'http://a.91jfk.com/Product/Ordershop/AllOrderData?p=' + page,
						data: {
							state: 6
						},
						dataType: 'json',
						success: function(res) {
							console.log(res);
							getJfscCode(res.code);
							getJFSCPage(res.total);
							// data = JSON.parse(res);
							if(res.nowpage < res.total) {
								Flag2(true);
								if(res.code == '1') {
									var data = res.message;
									if(data.length > 0) {
										for(var i = 0; i < data.length; i++) {
											if(data[i].order_state == '退换货申请') {
												turnBack(data[i].orderid, data[i].order_state, data[i].detail,res.nowpage, data[i].count, data[i].order_pay_price, data[i].order_pay_integral);
											}
											$('img.lazy'+res.nowpage).lazyload({
		                                        placeholder:"/Public/Product/img/loading2.gif",
		                                        effect: "fadeIn",
		                                        container: $(".allOrderList")
		                                    });
										}
										if(data.length < 3) {
											// 显示无数据
											me.noData();
											// 重置
											setTimeout(function() {
												me.resetload();
											}, 1500);
										}
									}
									return_goods(); // 申请退换货
									setTimeout(function() {
										// 重置
										me.resetload();
									}, 1500);
								} else {
									me.noData();
									// 重置
									setTimeout(function() {
										me.resetload();
									}, 1500);
								}
							} else {
								load2.abort();
								Flag2(false);
								if(res.code == '1') {
									var data = res.message;
									for(var i = 0; i < data.length; i++) {
										if(data[i].order_state == '退换货申请') {
											turnBack(data[i].orderid, data[i].order_state, data[i].detail,res.nowpage, data[i].count, data[i].order_pay_price, data[i].order_pay_integral);
										}
										$('img.lazy'+res.nowpage).lazyload({
	                                        placeholder:"/Public/Product/img/loading2.gif",
	                                        effect: "fadeIn",
	                                        container: $(".allOrderList")
	                                    });
									}
									if(data.length < 3) {
										// 显示无数据
										me.noData();
										// 重置
										setTimeout(function() {
											me.resetload();
										}, 1500);
									}
									
								} else {
									me.noData();
                                    me.lock();
									// 重置
									setTimeout(function() {
										me.resetload();
									}, 1500);
								}
							}
							setTimeout(function() {
								me.resetload();
							}, 1500);
						},
						// 加载出错
						error: function(err) {
							console.log('Ajax error!');
							console.log(err);
							// 即使加载出错，也得重置
							setTimeout(function() {
								me.resetload();
							}, 1500);
						}
					});
				} else {
					var max = compare(zeroPage, jfscPage);
					if(page > max) {
						// 再往下已经没有数据  锁定
						// 显示无数据
						me.noData();
						me.lock();
						// 重置
						setTimeout(function() {
							me.resetload();
						}, 1500);
					} else {
						me.unlock();
					}
				}
			}
		});
	}
	setTimeout(function(){
		if (zeroCode == 0 && JfscCode == 0) {
			$('.no_order').show();
			$('.no_order .img2').bind('click',function(){
				window.location.href = 'http://a.91jfk.com/Product/Pointshop/point_shop_index.html'
			});
            setTimeout(function(){
                $('div.dropload-noData').fadeOut(1000);
            },200);
		} else{
			$('.kong').hide();
		}
	},1500);

	/*参数
	 * orderNum		订单编号
	 * state		订单状态
	 * src			商品图片地址
	 * title		商品名称
	 * price		商品单价
	 * states		商品交易状态
	 * count		单件商品的数量
	 * num			单笔订单里的商品总数
	 * total		单笔订单的总价
	 */

	//退换货
	function zeroturnBack(orderNum, state, arr, j, num, total) {
		var List = $('.allOrderList');
		var listBox = $('<div class="orderList_box"></div>');
		var list_top = $('<div class="orderList_num"><span>订单编号：</span><span class="orderNum">' + orderNum + '</span><span class="TrsnState">' + state + '</span></div>');
		listBox.append(list_top);
		for(var i = 0; i < arr.length; i++) {
			var list_con = $('<div class="orderList_con"><div class="orderList_con_lt"><div class="lt_imgBox"><img class="IMG lazy'+ j +'" src="/Public/Product/img/loading2.gif" data-original="' + arr[i].thumbnail + '" alt="" /></div></div><div class="orderList_con_rt"><p class="p_top"><span class="goodTitle">' + arr[i].title + '</span><span class="red price">&yen;' + arr[i].price + '</span></p><p class="p_bot"><span class="sucesORfail">' + arr[i].states + '</span><span class="red count">x' + arr[i].count + '</span></p></div></div>');
			listBox.append(list_con);
		}
		var list_bot = $('<div class="orderList_bot"><p class="num-money">共<span class="good_piece pur">' + num + '</span>件商品，合计：&yen;<span class="allPrice pur">' + total + '元</span>(含运费&yen;3.0)</p><button class="return_goods fr">申请退货</button></div>');
		listBox.append(list_bot);
		List.append(listBox);
	}

	//退换货
	function turnBack(orderNum, state, arr, j, num, total1, total2) {
		var List = $('.allOrderList');
		var listBox = $('<div class="orderList_box"></div>');
		var list_top = $('<div class="orderList_num"><span>订单编号：</span><span class="orderNum">' + orderNum + '</span><span class="TrsnState">' + state + '</span></div>');
		listBox.append(list_top);
		for(var i = 0; i < arr.length; i++) {
			if(arr[i].cash_price == '0.00' && arr[i].point_price != '0') { //纯集分商品
				var list_con = $('<div class="orderList_con"><div class="orderList_con_lt"><div class="lt_imgBox"><img class="IMG lazy'+ j +'" src="/Public/Product/img/loading2.gif" data-original="' + arr[i].show_pic + '" alt="" /></div><span class="branchid">' + arr[i].branchid + '</span></div><div class="orderList_con_rt"><p class="p_top"><span class="goodTitle">' + arr[i].pname + '</span><span class="red price">&yen;' + arr[i].point_price + '集分</span></p><p class="p_bot"><span class="sucesORfail">' + arr[i].shuxing_zhi.replace(/'/g,'') + '</span><span class="red count">x<m>' + arr[i].purchase_num + '</m></span></p></div></div>');
				listBox.append(list_con);
			} else if(arr[i].cash_price != '0.00' && arr[i].point_price != '0') { //集分加现金商品
				var list_con = $('<div class="orderList_con"><div class="orderList_con_lt"><div class="lt_imgBox"><img class="IMG lazy'+ j +'" src="/Public/Product/img/loading2.gif" data-original="' + arr[i].show_pic + '" alt="" /></div><span class="branchid">' + arr[i].branchid + '</span></div><div class="orderList_con_rt"><p class="p_top"><span class="goodTitle">' + arr[i].pname + '</span><span class="red price">&yen;<m>' + arr[i].cash_price + '</m>元+<n>' + arr[i].point_price + '<n>集分</span></p><p class="p_bot"><span class="sucesORfail">' + arr[i].shuxing_zhi.replace(/'/g,'') + '</span><span class="red count">x<m>' + arr[i].purchase_num + '</m></span></p></div></div>');
				listBox.append(list_con);
			} else if(arr[i].cash_price != '0.00' && arr[i].point_price == '0') { //纯现金商品
				var list_con = $('<div class="orderList_con"><div class="orderList_con_lt"><div class="lt_imgBox"><img class="IMG lazy'+ j +'" src="/Public/Product/img/loading2.gif" data-original="' + arr[i].show_pic + '" alt="" /></div><span class="branchid">' + arr[i].branchid + '</span></div><div class="orderList_con_rt"><p class="p_top"><span class="goodTitle">' + arr[i].pname + '</span><span class="red price">&yen;<m>' + arr[i].cash_price + '</m>元</span></p><p class="p_bot"><span class="sucesORfail">' + arr[i].shuxing_zhi.replace(/'/g,'') + '</span><span class="red count">x<m>' + arr[i].purchase_num + '</m></span></p></div></div>');
				listBox.append(list_con);
			}
		}
		if(total1 != '0.00' && total2 == '0') { //纯现金商品
			var list_bot = $('<div class="orderList_bot"><p class="num-money">共<span class="good_piece pur">' + num + '</span>件商品，合计：&yen;<span class="allPrice pur">' + total1 + '元</span>(含运费&yen;3.0)</p></div>');
			listBox.append(list_bot);
		} else if(total1 == '0.00' && total2 != '0') { //纯集分商品
			var list_bot = $('<div class="orderList_bot"><p class="num-money">共<span class="good_piece pur">' + num + '</span>件商品，合计：&yen;<span class="allPrice pur">' + total2 + '集分</span>(含运费&yen;3.0)</p></div>');
			listBox.append(list_bot);
		} else if(total1 != '0.00' && total2 != '0') { //集分加现金商品
			var list_bot = $('<div class="orderList_bot"><p class="num-money">共<span class="good_piece pur">' + num + '</span>件商品，合计：&yen;<span class="allPrice pur">' + total1 + '元+' + total2 + '集分</span>(含运费&yen;3.0)</p></div>');
			listBox.append(list_bot);
		}
		List.append(listBox);
	}


});